# 雷达设计 v0.0.1
import argparse
from typing import Dict
import numpy as np
import matplotlib.pyplot as plt
from conf.app_config import AppConfig as AC
from irlab.basics.radar_equation import RadarEquation

class RadarDesignV001(object):
    def __init__(self):
        self.name = 'apps.rds.basics.s01009.RadarDesignV001'

    @staticmethod
    def design():
        print(f'雷达设计 v0.0.1')

def main(params:Dict = {}) -> None:
    RadarDesignV001.design()
    delta_R = 150 # 距离分辨率，单位：米
    # 2.1. 求脉冲宽度
    tau = 2 * delta_R / AC.C
    print(f'脉冲宽度：tau={tau};')
    # 2.2. 求带宽
    B = AC.C / (2*delta_R)
    print(f'带宽：B={B};')
    # 2.3. 确定参数
    f = 3E9 # 雷达频率，3GHz
    A_e = 2.25 # 有效孔径，单位：m^2
    # 2.4. 确定脉冲能量
    lambda_ = AC.C / f
    G = (4 * np.pi * A_e) / (lambda_**2)
    G_db = 10 * np.log10(G)
    print(f'波长：{lambda_}; 天线增益：{G}; {G_db};')
    # 2.5. 确定最小警戒距离
    R_min = 30E3 # 最小净空距离，单位：米
    v_a = 400 # 飞机飞行速度，单位：米/秒
    v_m = 150 # 导弹飞行速度，单位：米/秒
    v_i = 250 # 拦截器飞行速度（水平方向），单位：米/秒
    T_intercept = R_min / v_i
    R_a_p = R_min + v_a * T_intercept
    R_m_p = R_min + v_m * T_intercept
    print(f'R_a_p: {R_a_p}; R_m_p: {R_m_p};')
    R_a = 90E3
    R_m = 55E3
    # 2.6. 确定PRF
    R_u = max(R_a, R_m)
    PRF_ = AC.C / (2*R_u)
    print(f'PRF_: {PRF_};')
    PRF = 1000 # 单位：Hz
    # 2.7. 确定搜索体积
    h_a = 7E3 # 飞机飞行高度
    h_m = 2E3 # 导弹飞行高度
    theta_1 = np.arctan(h_m / R_m) / np.pi * 180
    theta_2 = np.arctan(h_a / R_min) / np.pi * 180
    print(f'theta_1: {theta_1}; theta_2: {theta_2};')
    Theta_a = 360 # 水平扫描角度范围
    Omega = (theta_2 - theta_1) / (57.296**2) * Theta_a
    print(f'Omega: {Omega};')
    # 2.8. 计算水平波束宽度
    theta_e = theta_2 - theta_1
    theta_a = (4*np.pi)/(G*(theta_e/180.0*np.pi)) / np.pi * 180
    print(f'theta_e: {theta_e}; theta_a: {theta_a};')
    # 2.9. 求累积脉冲数
    T_sc = 2
    dot_theta_scan = Theta_a / T_sc
    n_p = int(theta_a * PRF / dot_theta_scan)
    print(f'n_p: {n_p};')
    # 2.10. 求出$\underline{SNR}_{1}$
    SNR_NCI_db = 13
    SNR_NCI = 10**(SNR_NCI_db / 10.0)
    SNR_1 = SNR_NCI / (2*n_p) + np.sqrt( SNR_NCI**2/(4*n_p**2) + SNR_NCI/n_p )
    SNR_1_db = 10*np.log10(SNR_1)
    print(f'SNR_NCI: {SNR_NCI}; SNR_1: {SNR_1}; SNR_1_db: {SNR_1_db};')
    L_NCI = (1+SNR_1) / SNR_1
    L_NCI_db = 10*np.log10(L_NCI)
    print(f'L_NCI: {L_NCI}; L_NCI_db: {L_NCI_db};')
    # 2.11. 求脉冲能量
    F_db = 6 # 噪声数，单位：dB
    F = 10**(F_db/10.0)
    L_db = 8 # 损失，单位：dB
    L = 10**(L_db/10.0)
    T_e = 290
    sigma_m = 0.5
    sigma_a = 4
    E_m = (4*np.pi)**3 * AC.k * T_e * F * L * R_m**4 * SNR_1 / (G**2 * lambda_**2 * sigma_m)
    print(f'    {AC.k}; {R_m}; {SNR_1};')
    E_a = (4*np.pi)**3 * AC.k * T_e * F * L * R_a**4 * SNR_1 / (G**2 * lambda_**2 * sigma_a)
    print(f'E_m: {E_m}; E_a: {E_a};')
    E = max(E_a, E_m)
    P_t = E / tau
    print(f'P_t: {P_t};')
    ## 2.12. 绘制图像（以飞机为例）
    Rs = np.linspace(10E3, 100E3, num=1000)
    SNRs_a = 10 * np.log10( (P_t * G**2 * lambda_**2 * sigma_a)/((4*np.pi)**3 * AC.k * T_e * B * F * L * Rs**4) * (n_p / L_NCI) )
    SNR0s_a = RadarEquation.radar_equation(P_t, f, G, sigma_a, T_e, B, F, L, Rs)
    plt.figure(1)
    plt.plot(Rs/1000.0, SNRs_a, 'k-', label='NCI 7 Pulses')
    plt.plot(Rs/1000.0, SNR0s_a, 'k:', label='Single Pulse')
    # 设置坐标轴范围
    plt.axis([10, 100, 0, 50])
    # 显示网格
    plt.grid(True)
    # 添加图例
    plt.legend()
    # 添加轴标签
    plt.xlabel('Detection range - Km')
    plt.ylabel('SNR - dB')
    plt.title('Radar Design v0.0.1')
    plt.show()

def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser()
    parser.add_argument(
        '--run_mode', action='store',
        type=int, default=1, dest='run_mode',
        help='run mode'
    )
    return parser.parse_args()

if '__main__' == __name__:
    args = parse_args()
    params = vars(args)
    main(params=params)